﻿// WebZHX.Infrastructure/Services/FlourBatchOutNewService.cs
using Common.Models;
using Core.Entities;
using Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services
{
    /// <summary>
    /// 面粉出库批次服务
    /// </summary>
    public class FlourBatchOutNewService : BaseService<FlourBatchOutNew, int>
    {
        private readonly IBaseRepository<FlourBatchOutNew, int> _repository;
        private readonly IPagedRepository<FlourBatchOutNew, int> _pagedRepository;
        private readonly IDapperRepository _dapperRepository;

        public FlourBatchOutNewService(
            IBaseRepository<FlourBatchOutNew, int> repository,
            IPagedRepository<FlourBatchOutNew, int> pagedRepository,
            IDapperRepository dapperRepository)
            : base(repository, pagedRepository)
        {
            _repository = repository;
            _pagedRepository = pagedRepository;
            _dapperRepository = dapperRepository;
        }

        /// <summary>
        /// 根据批次号查询
        /// </summary>
        /// <param name="batchNumber">批次号</param>
        /// <returns>出库批次列表</returns>
        public async Task<IEnumerable<FlourBatchOutNew>> GetByBatchNumberAsync(string batchNumber)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "BatchNumber", Operator = "=", Value = batchNumber }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 获取指定日期范围内的出库记录
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>出库批次列表</returns>
        public async Task<IEnumerable<FlourBatchOutNew>> GetByDateRangeAsync(DateTime startDate, DateTime endDate)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "OutTime", Operator = ">=", Value = startDate },
                    new FilterCondition { Field = "OutTime", Operator = "<=", Value = endDate }
                },
                OrderBy = "OutTime",
                IsAscending = false
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 使用Dapper执行复杂查询 - 获取出库统计
        /// </summary>
        /// <returns>出库统计信息</returns>
        public async Task<dynamic> GetOutStatisticsAsync()
        {
            var sql = @"
                SELECT 
                    COUNT(1) as TotalCount,
                    SUM(DrawnWeight) as TotalDrawnWeight,
                    SUM(TotalWeight) as TotalWeightSum,
                    SUM(RemainingWeight) as TotalRemainingWeight,
                    AVG(DrawnWeight) as AverageDrawnWeight
                FROM FlourBatchOut_new 
                WHERE IsDeleted = 0";

            return await _dapperRepository.QueryFirstOrDefaultAsync<dynamic>(sql);
        }
    }
}